/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chipwits.room;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author Emmanuel
 */
public abstract class Sprite {
    private Image[] image;      //Tableau contenant les images successives de l'animation du sprite.
    private int frame;          //Image courante affichée par le sprite
    private Timer timer;        //Timer qui va incrémenter l'image affichée par le sprite
    private Component imageObs;     //image observer
    private Rectangle spaceOccupied;    //L'espace occupé par le sprite et sa position
    private Rectangle bounds;       //L'espace dans lequel peut évoluer le sprite
    private Point motionVector;     //La vitesse du sprite en x et en y
    private int direction;  //0:droite, 1:bas, 2:gauche, 3:haut, -1:Arret
    private Point positionCible;

    public Sprite(Point position, Image[] image, int frameDelay, Rectangle bounds, Component imageObs, boolean startAnim){
        this.image=image;
        frame=0;
        this.imageObs=imageObs;
        this.bounds=bounds;
        this.motionVector=new Point(0,0);
        this.positionCible=new Point(position);

        setSpaceOccupied(new Rectangle(position.x, position.y, image[0].getWidth(imageObs), image[0].getHeight(imageObs)));
        
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                incFrame();
            }
        };
        timer=new Timer(frameDelay, taskPerformer);
        if(startAnim)
            startAnim();
    }

    public void move(int direction, int speed){    //0:droite, 1:bas, 2:gauche, 3:haut
        this.direction=direction;
        //positionCible=new Point(this.spaceOccupied.x, this.spaceOccupied.y);
        switch(direction){
            case 0:
                this.motionVector=new Point(speed, 0);
                positionCible.translate(RoomPanel.TAILLE_CASE, 0);
                break;
            case 1:
                this.motionVector=new Point(0, speed);
                positionCible.translate(0, RoomPanel.TAILLE_CASE);
                break;
            case 2:
                this.motionVector=new Point(-speed, 0);
                positionCible.translate(-RoomPanel.TAILLE_CASE, 0);
                break;
            case 3:
                this.motionVector=new Point(0, -speed);
                positionCible.translate(0, -RoomPanel.TAILLE_CASE);
                break;
        }
    }

//ACCESSEURS
    public void changeImages(Image[] images){
        this.image=images;
    }

    public Rectangle getSpaceOccupied() {
        return spaceOccupied;
    }

    void setSpaceOccupied(Rectangle spaceOccupied) {
        this.spaceOccupied = spaceOccupied;
    }

    public void setSpaceOccupied(Point position) {
        this.spaceOccupied.setLocation(position.x, position.y);
    }

    public Point getMotionVector() {
        return motionVector;
    }

    public void setMotionVector(Point motionVector) {
        this.motionVector = motionVector;
    }

//GESTION DU TRACE

    private boolean isPositionOK(Point a, Point b, Point speed){
        if(Math.abs(a.x-b.x)<=Math.abs(speed.x) && Math.abs(a.y-b.y)<=Math.abs(speed.y)){
            return true;
        }
        return false;
    }

    public void updatePosition(){
        if(direction<0){
           return;
        }
        Point position = new Point(spaceOccupied.x, spaceOccupied.y);
        position.translate(motionVector.x, motionVector.y);
        if(isPositionOK(position, positionCible, motionVector)){
            direction=-1;
            setMotionVector(new Point(0,0));
            setSpaceOccupied(positionCible);
            return;
        }
        setSpaceOccupied(position);
    }

    void drawSpriteImage(Graphics g) {
        g.drawImage(image[frame], spaceOccupied.x, spaceOccupied.y, imageObs);
    }


//GESTION DE L'ANIMATION
    public void incFrame(){
        frame++;
        if(frame>=image.length)
            frame=0;
    }

    public void changeFrameDelay(int frameDelay){
        timer.setDelay(frameDelay);
    }

    public void stopAnim(){
        if(timer.isRunning()) timer.stop();
    }

    public void startAnim(){
        if(!timer.isRunning()) timer.start();
    }

    public void setSpeedAnim(int speed){
        timer.setDelay(speed);
    }
}
